package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OjdbcUtil2 {
	/*private static String url = "jdbc:oracle:thin:@192.168.95.131:1521:XE";
	private static String user = "system";
	private static String password = "f123456";*/
	private static String url = "jdbc:oracle:thin:@192.168.8.103:1521:LDRK";
	private static String user = "mansys";
	private static String password = "mansys";
	private static Connection connJdbc = null;
	private static PreparedStatement pstmt = null;
	private static ResultSet rs = null;
	
public static Connection getConn() {
	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");
	} catch (ClassNotFoundException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	
	try {
		connJdbc = DriverManager.getConnection(url,user,password);
		System.out.println(connJdbc);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return connJdbc;
}

public static void close(ResultSet rs,PreparedStatement psmt, Connection conn){
	if(rs !=null){
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	if(psmt != null){
		try {
			psmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	if(conn != null){
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


public static int executeUpdate(String sql, Object... objects) {
	try {
		// 1. 得到连接对象
		connJdbc = getConn();
		// 2. 得到Statement对象
		 pstmt = connJdbc.prepareStatement(sql);
		 System.out.println(objects != null);
		if (objects != null) {
			// 依次设置参数
			for (int i = 0; i < objects.length; i++) {
				pstmt.setObject(i+1, objects[i]);
			}
		}
		System.out.println(sql);
		// 3. 执行SQL语句得到结果
		int rows = pstmt.executeUpdate();
		System.out.println("rows"+rows);
		// 4. 释放资源
		close(null, pstmt, connJdbc);
		// 5. 返回执行的结果
		return rows;
	} catch (SQLException e) {
		return -1;
	}
}
	
	
	
}
